package com.shell.customer.accreditation.rx;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.customer.accreditation.AccreditEvent;
import com.shell.customer.accreditation.vo.AccreditationVo;
import com.shell.customer.accreditation.vo.UserVo;
import com.shell.customer.esign.service.EsignAcctTxService;
import com.shell.customer.po.CusRec;
import com.shell.customer.util.CustomerUtil;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.timevale.esign.sdk.tech.bean.result.AddAccountResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EsignOpenAcctSubscriber extends OnErrorNotImplementedSubscriber<AccreditEvent> {
	@Autowired
	private EsignAcctTxService esignAcctTxService;

	public void onApplicationEvent(AccreditEvent event) {
		if (CustomerUtil.onEsign()) {
			AccreditationVo accreditationVo = event.getAccreditationVo();
			CusRec cus = accreditationVo.getCustomerC();
			UserVo admin = accreditationVo.getAdministrator();
			AddAccountResult result = null;
			if (!"PERSONAL".equals(cus.getCusType())) {
				result = this.esignAcctTxService.openCustAcct(cus, admin);
				if (result.getErrCode() != 0) {
					throw new UnCheckMsgException("����e������������������" + result.getMsg());
				}
			} else {
				result = this.esignAcctTxService.openPersonAcct(admin);
				if (result.getErrCode() != 0) {
					throw new UnCheckMsgException("����e������������������" + result.getMsg());
				}
			}
			event.setValue("acctId", result.getAccountId());
		}
	}
}
